package com.transpeed.payservice.webDaoImpl;

import java.io.IOException;

import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.json.JSONObject;

import com.transpeed.payservice.Utils.JsonUtils;
import com.transpeed.payservice.log.InitLogger;
import com.transpeed.payservice.log.Log;
import com.transpeed.payservice.main.PostMessage;
import com.transpeed.payservice.model.AliPayCarInfo;
import com.transpeed.payservice.model.AlipayNotifyInfo;
import com.transpeed.payservice.pay.alipay.GetAlipayQRCode;
import com.transpeed.payservice.pay.alipay.QueryAlipayTrade;

/**
 * TODO 支付宝相关API
 * GetAlipayQRCode 获取支付宝二维码
 * QueryAlipayTrade 查询支付宝订单
 * ReceiveAliPayTradeInfo 接收支付宝支付结果异步通知
 * @author lizhi
 * @date 2017年4月25日
 * 更新日志
 * 2017年4月25日 lizhi 首次创建
 *
 */

@Path("/AlipayAPI")
public class AlipayAPI {
	@POST
	@Path("/GetAlipayQRCode")
	@Produces(MediaType.TEXT_HTML)
	
	// @Produces(MediaType.APPLICATION_JSON)
	// @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	// 本地测试
	// http://192.168.0.102:8080/TranSpeed-PayService/AlipayAPI/GetAlipayQRCode
	// 服务器测试
	// http://122.97.128.110:8080/TranSpeed-PayService/AlipayAPI/GetAlipayQRCode
	// 物联网测试服务器
	// http://192.168.254.5:8080/TranSpeed-PayService/AlipayAPI/GetAlipayQRCode
	
	/**
	 * @author 用于接收上位机获取支付宝扫码请求 important!!!
	 */

	public String GetAlipayQRCode(
			@FormParam("TotalAmount") String TotalAmount,
			@FormParam("CarInPlace") String CarInPlace,
			@FormParam("CarOutPlace") String CarOutPlace,
			@FormParam("CarPlate") String CarPlate,
			@FormParam("CarInTime") String CarInTime,
			@FormParam("url") String NotifyUrl

	) throws IOException {

		AliPayCarInfo ParkInfo = new AliPayCarInfo();
		ParkInfo.setTotalAmount(TotalAmount);
		ParkInfo.setCarInPlace(CarInPlace);
		ParkInfo.setCarOutPlace(CarOutPlace);
		ParkInfo.setCarPlate(CarPlate);
		ParkInfo.setCarInTime(CarInTime);
		ParkInfo.setNotifyUrl(NotifyUrl);

		InitLogger.Createfolder();

		Log.Write("收到上位机生成二维码请求！", "AlipayQRCode");
		Log.Write("收到上位机车辆信息为：\n" + JsonUtils.formatAsJSON(new JSONObject(ParkInfo).toString()), "AlipayQRCode");
		Log.Write("开始向Alipay传送生成二维码请求！", "AlipayQRCode");
		
		String Subject = CarPlate + " (" + CarInPlace + ")";//定义支付宝显示订单名字
		
		String QRurl = GetAlipayQRCode.test_trade_precreate(TotalAmount , Subject , NotifyUrl);
		System.out.println(NotifyUrl);
		Log.Write("二维码生成成功！已推送给上位机!返回信息为：" + QRurl, "AlipayQRCode");

		return QRurl;
	}

	/**
	 * @author 用于接收上位机查询支付宝订单请求
	 * 
	 */
	@POST
	@Path("/QueryAlipayTrade")
	@Produces(MediaType.TEXT_HTML)
	// @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	// 本地测试
	// http://192.168.0.102:8080/TranSpeed-PayService/AlipayAPI/QueryAlipayTrade
	// 服务器测试
	// http://122.97.128.110:8080/TranSpeed-PayService/AlipayAPI/QueryAlipayTrade
	// 物联网测试服务器
	// http://192.168.254.5:8080/TranSpeed-PayService/AlipayAPI/QueryAlipayTrade
	public String QueryAlipayTrade(
			@FormParam("TradeNumber") String TradeNumber)
			throws IOException {

		
		if(TradeNumber.equals("失败"))
			return "Alipay&Failed&107";
		
		Log.Write("收到上位机查询支付宝订单状态请求！ 订单号为：" + TradeNumber, "AlipayQRCode");
		String TradeStatus = QueryAlipayTrade.test_trade_query(TradeNumber);
		Log.Write("已通知上位机！订单查询结果为：" + TradeStatus, "AlipayQRCode");
		return TradeStatus;
	}

	
	/**
	 * @author lizhi 用于接收用户付款成功后支付宝回传信息
	 */

	@POST
	@Path("/ReceiveAliPayTradeInfo")
	@Produces(MediaType.TEXT_HTML)
	// @Produces(MediaType.APPLICATION_JSON)
	// @Consumes(MediaType.APPLICATION_JSON)
	// 访问方法路径
	// http://122.97.128.110:8080/TranSpeed-PayService/AlipayAPI/ReceiveAliPayTradeInfo
	public String ReceiveAliPayTradeInfo(
			@FormParam("notify_time") String notify_time,
			@FormParam("notify_type") String notify_type,
			@FormParam("notify_id") String notify_id,
			@FormParam("sign_type") String sign_type,
			@FormParam("sign") String sign,
			@FormParam("trade_no") String trade_no,
			@FormParam("app_id") String app_id,
			@FormParam("out_trade_no") String out_trade_no,
			@FormParam("out_biz_no") String out_biz_no,
			@FormParam("buyer_id") String buyer_id,
			@FormParam("buyer_logon_id") String buyer_logon_id,
			@FormParam("seller_id") String seller_id,
			@FormParam("seller_email") String seller_email,
			@FormParam("trade_status") String trade_status,
			@FormParam("total_amount") String total_amount,
			@FormParam("receipt_amount") String receipt_amount,
			@FormParam("invoice_amount") String invoice_amount,
			@FormParam("buyer_pay_amount") String buyer_pay_amount,
			@FormParam("point_amount") String point_amount,
			@FormParam("refund_fee") String refund_fee,
			@FormParam("send_back_fee") String send_back_fee,
			@FormParam("subject") String subject,
			@FormParam("body") String body,
			@FormParam("gmt_create") String gmt_create,
			@FormParam("gmt_payment") String gmt_payment,
			@FormParam("gmt_refund") String gmt_refund,
			@FormParam("gmt_close") String gmt_close,
			@FormParam("fund_bill_list") String fund_bill_list)
			
			throws IOException {

		InitLogger.Createfolder();

		Log.Write("收到支付宝推送！", "AlipayQRCode");
		AlipayNotifyInfo AlipayInfo = new AlipayNotifyInfo();

		AlipayInfo.setNotify_time(notify_time);
		AlipayInfo.setNotify_type(notify_type);
		AlipayInfo.setApp_id(app_id);
		AlipayInfo.setOut_trade_no(out_trade_no);
		AlipayInfo.setOut_biz_no(out_biz_no);
		AlipayInfo.setBuyer_id(buyer_id);
		AlipayInfo.setBuyer_logon_id(buyer_logon_id);
		AlipayInfo.setSeller_id(seller_id);
		AlipayInfo.setSeller_email(seller_email);
		AlipayInfo.setTrade_status(trade_status);
		AlipayInfo.setTotal_amount(total_amount);
		AlipayInfo.setReceipt_amount(receipt_amount);
		AlipayInfo.setInvoice_amount(invoice_amount);
		AlipayInfo.setBuyer_pay_amount(buyer_pay_amount);
		AlipayInfo.setPoint_amount(point_amount);
		AlipayInfo.setRefund_fee(refund_fee);
		AlipayInfo.setSend_back_fee(send_back_fee);
		AlipayInfo.setSubject(subject);
		AlipayInfo.setBody(body);
		AlipayInfo.setGmt_create(gmt_create);
		AlipayInfo.setGmt_payment(gmt_payment);
		AlipayInfo.setGmt_refund(gmt_refund);
		AlipayInfo.setGmt_close(gmt_close);
		AlipayInfo.setFund_bill_list(fund_bill_list);
		
		Log.Write("收到支付宝推送信息为：\n" + JsonUtils.formatAsJSON(new JSONObject(AlipayInfo).toString()), "AlipayQRCode");
		String TradeStatusSuccess = null;
		
		String NotifyUrl = body;
		System.out.println(NotifyUrl);
		
		Log.Write("此条通知的支付状态：" + trade_status, "AlipayQRCode");

		if (trade_status.equals("WAIT_BUYER_PAY"))
			Log.Write("用户已扫码！", "AlipayQRCode");
		if (trade_status.equals("TRADE_SUCCESS")) {
			TradeStatusSuccess = "Alipay&Success&" + out_trade_no;
			Log.Write("开始向上位机推送成功信息！信息为：" + TradeStatusSuccess, "AlipayQRCode");
			Log.Write("推送上位机地址为：" + NotifyUrl, "AlipayQRCode");
			
			String info = PostMessage.AlipaySeverPost(TradeStatusSuccess , NotifyUrl, "AlipayQRCode");

			if (info!=null&&info.equals("Success"))
				Log.Write("向上位机推送信息成功！", "AlipayQRCode");
			else
				Log.Write("向上位机推送信息失败！", "AlipayQRCode");

		}

		return "success";//回传支付宝告知已收到
	}

}
